JavaScript recognizes the following types of values:
ΓÇónumbers, such as 42 or 3.14159 ΓÇólogical (Boolean) values, either true or false ΓÇóstrings, such as "Howdy!" ΓÇónull, a special keyword denoting a null value
This relatively small set of types of values, or data types, enables you to perform useful functions with your applications. Notice that there is no explicit distinction between integer and real-valued numbers. Nor is there an explicit date data type. However, the date object and related built-in functions enable you to handle dates.
Objects and functions are the other fundamental elements in the language. You can think of objects as named containers for values, and functions as procedures that your application can perform.
Datatype Conversion
JavaScript is a loosely typed language. That means that you do not have to specify the datatype of a variable when you declare it, and datatypes are converted automatically as needed during the course of script execution.
JavaScript will attempt to convert an expression to the datatype of the left-hand operand. Expressions are always evaluated from left to right, so JavaScript applies this rule at each step in the evaluation of a complex expression.
For example, suppose you define the following variables
var astring = "7"
var anumber = 42
Then consider the following statements:
x = astring + anumber
y = anumber + astring
The first statement will convert anumber to a string value, because the left-hand operand, astring, is a string. The statement will then concatenate the two strings, so x will have a value of "742".
Conversely, the second statement will convert astring to a numeric value, because the left-hand operand, anumber, is a number. The statement then adds the two numbers, so y will have a value of 49.
JavaScript cannot convert some strings to numbers. For example, the statements
var anumber = 42
var astring = "Phil"
y = anumber + astring
will generate an error, becuase "Phil" cannot be converted to a number.
The following table summarizes conversion between data types.
NOTE: Much of the functionality specified in this table is not implemented as of Navigator beta4.
You use variables to hold values in your application. You give these variables names by which you reference them, and there are certain rules to which the names must conform.
A JavaScript identifier or name must start with a letter or underscore ("_"); subsequent characters can also be digits (0-9). Letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase). JavaScript is case-sensitive.
Literals are the way you represent values in JavaScript. These are fixed values that you literally provide in your application source, and are not variables. Examples of literals include:
ΓÇó42 ΓÇó3.14159 ΓÇó"To be or not to be"
Integers
Integers can be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8) format. A decimal integer literal consists of a sequence of digits (optionally suffixed as described below) without a leading 0 (zero).
An integer can be expressed in octal or hexadecimal rather than decimal. A leading 0 (zero) on an integer literal means it is in octal; a leading 0x (or 0X) means hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F. Octal integers can include only the digits 0-7.
Floating Point Literals
A floating point literal can have the following parts: a decimal integer, a decimal point ("."), a fraction (another decimal number), an exponent, and a type suffix. The exponent part is an "e" or "E" followed by an integer, which can be signed (preceded by a "+" or "-"). A floating point literal must have at least one digit, plus either a decimal point or "e" (or "E"). Some examples of floating point literals are:
ΓÇó3.1415 ΓÇó-3.1E12 ΓÇó.1e12 ΓÇó2E-12
Boolean Literals
The boolean type has two literal values: true and false.
String Literals
A string literal is zero or more characters enclosed in double (") or single (') quotes. A string must be delimited by quotes of the same type; that is, either both single quotes or double quotes. The following are examples of string literals:
ΓÇó"blah" ΓÇó'blah' ΓÇó"1234" ΓÇó"one line \n another line"
Special Characters
You can use the following special characters in JavaScript string literals:
ΓÇó\b indicates a backspace. ΓÇó\f indicates a a form feed. ΓÇó\n indicates a new line character. ΓÇó\r indicates a carriage return. ΓÇó\t indicates a tab character.